package com.yangmao.mvc.controller;

import com.yangmao.entity.Admin;
import com.yangmao.service.api.AdminService;
import constant.SystemConstant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import util.ResultEntity;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author ‘洋毛’
 * @version 1.0
 * @description: TODO
 * @date 2021/12/11 11:13
 */
@Controller
public class AdminController {

    @Autowired
    private AdminService adminService;

    Logger logger = LoggerFactory.getLogger(AdminController.class);

    @ResponseBody
    @RequestMapping("/admin/get/all.json")
    public ResultEntity<List<Admin>> getAllAdmin() {
        List<Admin> allAdmin = adminService.getAllAdmin();
        for(Admin admin : allAdmin) {
            System.out.println(admin);
        }
        return ResultEntity.successWithData(allAdmin, allAdmin.size());
    }

    @RequestMapping("/admin/do/login.html")
    public String doLogin(@RequestParam("loginAcct")String loginAcct, @RequestParam("userPswd")String userPswd, HttpSession session){

        Admin admin = adminService.getAdminByAcct(loginAcct, userPswd);

        session.setAttribute(SystemConstant.ATTR_NAME_LOGIN_ADMIN,admin);

        return "redirect:/admin/to/main/page.html";



    }
}
